'use strict';
var fs = require("fs");
var sqlite3 = require("sqlite3");
var path = require('path');
var _ = require('underscore');
var moment = require('moment');
var config = require('./conf/config');
var db = new sqlite3.Database(config.database);
function add(db,obj, callback) {
	if (!obj) {
		callback && callback('params error');
		return ;
	}
	db.run("insert into biz_fileinfo(name, type,root, path, rel_path, dev, ino, mode, nlink, uid, gid, rdev, size, blksize, blocks, ctime, atime, mtime, birthtime,file_sha1,file_md5) values(?, ?,?, ?,?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?,?, ?)",
	[obj.name, obj.type,obj.root, obj.path, obj.rel_path, obj.dev, obj.ino, obj.mode, obj.nlink, obj.uid, obj.gid, obj.rdev, obj.size, obj.blksize, obj.blocks, obj.ctime, obj.atime, obj.mtime, obj.birthtime,obj.file_sha1,obj.file_md5]
	,function(err) {
		callback(err);
	  });
}

function batchAdd(objs, callback) {
	var db = new sqlite3.Database(config.database);
	if(!objs){ return;}
	db.run("BEGIN TRANSACTION;");
	objs.forEach(function(obj){
		//console.log(objs);
		add(db,obj,callback);	
	});
	db.run("COMMIT TRANSACTION;");
	db.close();
}

function delByRoot(db,root, callback) {
	if (!root) {
		callback && callback('params error');
		return ;
	}
	db.run("delete from biz_fileinfo where root=? ;",
	[root]
	,function(err, rows) {
		callback(err,rows);
	  });
}

function del(db,id, callback) {
	if (!id) {
		callback && callback('params error');
		return ;
	}
	db.run("delete from biz_fileinfo where id=? ;",
	[id]
	,function(err, rows) {
		callback(err,rows);
	  });
}
function update(db,obj, callback) {
	if (!obj) {
		callback && callback('params error');
		return ;
	}
	db.run("update biz_fileinfo set name=?,type=?,path=?,rel_path=?,dev=?,ino=?,mode=?,nlink=?,uid=?,gid=?,rdev=?,size=?,blksize=?,type=?,blocks=?,atime=?,mtime=?,file_sha1=?,file_md5=? where id= ?;",
	[obj.name, obj.type, obj.path,obj.rel_path, obj.dev, obj.ino, obj.mode, obj.nlink, obj.uid, obj.gid, obj.rdev, obj.size, obj.blksize, obj.blocks,moment(Date.now()).format("YYYY-MM-DD HH:mm:ss"), moment(Date.now()).format("YYYY-MM-DD HH:mm:ss"),obj.file_sha1,obj.file_md5,obj.id]
	,function(err, rows) {
		callback(err,rows);
	  });
}




function rename(db,obj, callback) {
	if (!obj) {
		callback && callback('params error');
		return ;
	}
	console.log("obj.rel_path"+obj.rel_path);
	db.run("update biz_fileinfo set name=?,type=?,rel_path=?,mtime=? where id= ? ;",
	[obj.name, obj.type,obj.rel_path,moment(Date.now()).format("YYYY-MM-DD HH:mm:ss"),obj.id]
	,function(err, rows) {
		callback(err,rows);
	  });
}
function updatePath(obj,fileName, callback) {
	var db = new sqlite3.Database(config.database);
	if (!obj||!fileName) {
		callback && callback('params error');
		return ;
	}
	db.run("BEGIN TRANSACTION;");
	var oldRelPath=obj.rel_path;
	var dirpath=path.dirname(oldRelPath);
	var newRelPath=dirpath+path.sep+fileName;
	console.log("newRelPath"+newRelPath);
	db.run("update biz_fileinfo set name=?,rel_path=?,mtime=? where id= ? ;",
	[fileName,newRelPath,moment(Date.now()).format("YYYY-MM-DD HH:mm:ss"),obj.id]
	,function(err, rows) {
		callback(err,rows);
	  });
	var oldPath=obj.path+"/"+obj.name;
	var newPath=obj.path+"/"+fileName;
	db.run("update biz_fileinfo set path=REPLACE(path,?,?) ,rel_path=REPLACE(rel_path,?,?) ,mtime=? where path like ? and rel_path like ? and root=? ;",
	  [oldPath, newPath,oldRelPath,newRelPath, moment(Date.now()).format("YYYY-MM-DD HH:mm:ss"),""+oldPath+"%",""+oldRelPath+"%",obj.root]
		,function(err, rows) {
			db.run("COMMIT TRANSACTION;");
		callback(err,rows);
	  });
}
function count(db,obj, callback) {
	if (!obj) {
		callback && callback('params error');
		return ;
	}
	var sql="select count(1) as totalCount from biz_fileinfo where 1=1 ";
	var name=obj.name;
	var path=obj.path;
	if(name&&name.length>0){
		sql+=" and path like ? and name like ?";
		db.all(sql,
		[path+"%","%"+name+"%"]
		,function(err, totalCount) {
		//  console.log(totalCount);
		  callback(err,totalCount);
		});
	}else{
	  sql+=" and path=? ";
	  db.all(sql, [path],function(err, totalCount) {
		//console.log(totalCount);
		callback(err,totalCount);
	  });
	}
}
function search(db,obj,sort,order, callback) {
	if (!obj) {
		callback && callback('params error');
		return ;
	}
	if (!sort) {
		sort="name";
	}
	if (!order) {
		order="DESC"
	}
	var sql="select id,name, path, type, dev, ino, mode, nlink, uid, gid, rdev, size, blksize, blocks, ctime, atime, mtime, birthtime,file_sha1,file_md5  from biz_fileinfo where 1=1 ";
	var name=obj.name;
	if(name&&name.length>0){
		sql+=" and name like ? ";
		if(sort&&sort.length>0){
			sql+=" order by "+sort + " "+order ;
		}
		db.all(sql,["%"+name+"%"]
		,function(err, rows) {
		  callback(err,rows);
		});
	}
	
}

function get(db,id, callback) {
	if (!id) {
		callback && callback('params error');
		return ;
	}
	var sql="select id, name,rel_path,path ,type, dev, ino, mode, nlink, uid, gid, rdev, size, blksize, blocks, ctime, atime, mtime, birthtime,file_sha1,file_md5  from biz_fileinfo where 1=1 ";
	if(id&&id.length>0){
		sql+=" and id=? ";
		db.get(sql,
		[id]
		,function(err, row) {
		  callback(err,row);
		});
	}
}
function getByPath(db,fullPath, callback) {
	if (!fullPath) {
		callback && callback('params error');
		return ;
	}
	var sql="select id, name,rel_path,path,root ,type, dev, ino, mode, nlink, uid, gid, rdev, size, blksize, blocks, ctime, atime, mtime, birthtime,file_sha1,file_md5  from biz_fileinfo where 1=1 ";
	if(fullPath&&fullPath.length>0){
		sql+=" and path||'/'||name= ? ";
		db.get(sql,
		[fullPath]
		,function(err, row) {
		  callback(err,row);
		});
	}
}

function getByRelPath(db,relPath, callback) {
	if (!relPath) {
		callback && callback('params error');
		return ;
	}
	var sql="select id, name,rel_path,path,root ,type, dev, ino, mode, nlink, uid, gid, rdev, size, blksize, blocks, ctime, atime, mtime, birthtime,file_sha1,file_md5  from biz_fileinfo where 1=1 ";
	if(relPath&&relPath.length>0){
		sql+=" and rel_path= ? ";
		db.get(sql,
		[relPath]
		,function(err, row) {
		  callback(err,row);
		});
	}
}


function query(db,obj,sort,order, callback) {
	if (!obj) {
		callback && callback('params error');
		return ;
	}
	var sql="select id, name, path,type, dev, ino, mode, nlink, uid, gid, rdev, size, blksize, blocks, ctime, atime, mtime, birthtime,file_sha1,file_md5  from biz_fileinfo where 1=1 ";
	var name=obj.name;
	var path=obj.path;
	if (!sort) {
		sort="name";
	}
	if (!order) {
		order="DESC"
	}
	
	if(name&&name.length>0){
		sql+=" and path like ? and name like ?";
		if(sort&&sort.length>0){
			sql+=" order by "+sort + " "+order ;
		}
		db.all(sql,
		[path+"%","%"+name+"%"]
		,function(err, rows) {
		//  console.log(rows);
		  callback(err,rows);
		});
	}else{
	  sql+=" and path=? ";
	  if(sort&&sort.length>0){
			sql+=" order by "+sort + " "+order ;
	  }
	  db.all(sql, [path],function(err, rows) {
		//console.log(rows);
		callback(err,rows);
	  });
	}
}
module.exports={
	add,
	del,
	update,
	query,
	get,
	count,
	search,
	batchAdd,
	rename,
	updatePath,
	getByRelPath,
	delByRoot,
	getByPath
}